<?php
namespace app\bis\controller;
use think\Controller;

class Deal extends Base
{
    private $obj;
	public function _initialize(){
		$this->obj = model("Deal");
		}
   
   
   
   
    public function index()
    {
      $account = session('bisAccount', '', 'bis');
				//var_dump($account);
				if($account && $account->bis_id){
					 $bis_id = $account->bis_id;
					// return $bis_id;
					}else{				
				    return $this->redirect('login/index');
					}
	  
	  $deal = $this ->obj->getDealByStatusBisid(0,$bis_id);
		return $this->fetch('', [
		    'deal' => $deal,
			'bis_id' => $bis_id,
		]);
	 
	}


    public function add()
    {
       $bisId = $this->getLoginUser()->bis_id;
	     	   
	   if(request()->isPost()){

		   $data = input('post.');
    
		// dump($data);
		  
		  //获取经纬度
		  $location = model('BisLocation')->get($data['location_ids'][0]); 
		  		  
		  
		  //获取二级分类
		  $data['cat'] = '';
		     
		  //商品基本信息入库
		 $deals = [
		     'bis_id' => $bisId,
			 'name' => $data['name'],
			 'image' => $data['image'],		 
		     'category_id' => $data['category_id'],
			// 'se_category_id' => empty($data['se_category_id']) ? '' : implode(',', $data['se_category_id']),    //se_category_id===input  命名有问题
			 'city_id' => $data['city_id'],
			 
			// 'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],	
			 
			 'location_ids' => empty($data['location_ids']) ? '' : implode(',', $data['location_ids']),
			 'start_time' => strtotime($data['start_time']),
			 'end_time' => strtotime($data['end_time']),
			 'total_count' => $data['total_count'],
			 'origin_price' => $data['origin_price'],
			 'current_price' => $data['current_price'],
			 'coupons_begin_time' => strtotime($data['coupons_begin_time']),
			 'coupons_end_time' => strtotime($data['coupons_end_time']),
			 'notes' => $data['notes'],
			 'description' => $data['description'],
			 'bis_account_id' => $this->getLoginUser()->id,
			 'xpoint' => $location->xpoint,
			 'ypoint' => $location->ypoint,
			 
			 
			  ];
			if(isset($data['id'])){
				$id = model('Deal')->updateById($deals, $data['id']);
				if($id){
			          $this->success('团购商品更新成功', url('deal/index'));
			      }else{
				      $this->error('团购商品更新失败');
				      }
				
				}else{
			
		        $id = model('Deal')->add($deals);
		   
		       if($id){
			          $this->success('添加团购商品成功', url('deal/index'));
			      }else{
				      $this->error('添加团购商品失败');
				      }
		   
				}

		  
		  
		  
		   }else{
	 
	 
	 //获取一级城市数据
		$citys = model('City')->getNormalCitysByParentId();
		
		//获取一级分类数据
		$categorys = model('Category')->getNormalCategorysByParentId();
		
		return $this->fetch('',[
		    'citys' => $citys,
			'categorys' => $categorys,
			'bislocation' => model('BisLocation')->getNormalLocationByBisId($bisId),
		]);	
	 }
	
	}














}
